package cn.shop.merchant.controller.bankcard;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import cn.shop.merchant.service.BankCardService;
import cn.shop.merchant.service.MerchantUserService;
import cn.shop.thiredparty.entity.AuthResult;
import cn.shop.thiredparty.entity.BankAuthCode;
import cn.shop.thiredparty.service.ThiredpartyService;
import cn.shop.transcation.bankcard.entity.BankCard;
import cn.shop.transcation.merchantUser.mapper.entity.MerchantUser;
import cn.shop.utils.ApiCode;
import cn.shop.utils.ApiResult;
import cn.shop.utils.annotation.OperationLog;
import cn.shop.utils.enums.OperationType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * 银行卡绑定接口
 * @author : 李一涵
 * @date : Created in 2022-2-16 14:58
 * @description :
 */
@RestController
@RequestMapping("/bankcard")
public class BankCardController {

    @Resource
    private BankCardService bankCardService;
    @Resource
    private ThiredpartyService thiredpartyService;
    @Resource
    private MerchantUserService userService;

    private static final int MAX_BANK_COUNT = 5;

    /**
     * 保存银行卡
     * @param bankCard 银行卡信息
     * @return 返回结果
     */
    @PostMapping("")
    @OperationLog(desc = "绑定银行卡", type = OperationType.CREATE)
    public ApiResult<Map<String, Object>> save(@Validated @RequestBody BankCard bankCard) {
        long uid = StpUtil.getSession().getLong(UID);
        long count = bankCardService.getBankCount(uid);
        if (count >= MAX_BANK_COUNT) {
            return ApiResult.fail(ApiCode.FAIL, "银行卡最多只能添加5个");
        }
        MerchantUser user = userService.getById(uid);
        if (StrUtil.isBlank(user.getName())) {
            return ApiResult.fail("请先完善基本信息");
        }
        if (StrUtil.isBlank(user.getIdentityCard())) {
            return ApiResult.fail("请先实名认证");
        }
        AuthResult result = thiredpartyService.bankAuth(bankCard.getCardNo(), user.getIdentityCard(), bankCard.getMobile(), user.getName());
        if (!BankAuthCode.C10000.code().equals(result.getCode())) {
            return ApiResult.fail("银行卡认证失败");
        }
        if (!AuthResult.State.SAME.code().equals(result.getData().getState())) {
            return ApiResult.fail("请检查银行卡信息是否正确");
        }
        bankCard.setOwnerId(uid);
        bankCard.setName(user.getName());
        return ApiResult.result(bankCardService.save(bankCard));
    }

    /**
     * 更新银行卡信息
     * @param bankCard 银行卡信息
     * @return 返回结果
     */
    @PutMapping("")
    @OperationLog(desc = "修改银行卡", type = OperationType.UPDATE)
    public ApiResult<Map<String, Object>> update(@Validated @RequestBody BankCard bankCard) {
        long uid = StpUtil.getSession().getLong(UID);
        MerchantUser user = userService.getById(uid);
        if (StrUtil.isBlank(user.getName())) {
            return ApiResult.fail("请先完善基本信息");
        }
        if (StrUtil.isBlank(user.getIdentityCard())) {
            return ApiResult.fail("请先实名认证");
        }
        AuthResult result = thiredpartyService.bankAuth(bankCard.getCardNo(), user.getIdentityCard(), bankCard.getMobile(), user.getName());
        if (!BankAuthCode.C10000.code().equals(result.getCode())) {
            return ApiResult.fail("银行卡认证失败");
        }
        if (!AuthResult.State.SAME.code().equals(result.getData().getState())) {
            return ApiResult.fail("请检查银行卡信息是否正确");
        }
        return ApiResult.result(bankCardService.updateInfo(uid, bankCard));
    }

    /**
     * 获取已绑定的银行卡列表
     * @return 返回结果
     */
    @GetMapping("")
    public ApiResult<List<Map<String, Object>>> list() {
        long uid = StpUtil.getSession().getLong(UID);
        List<Map<String, Object>> list = bankCardService.bankList(uid);
        return ApiResult.success(list);
    }

    /**
     * 获取银行卡信息
     * @param id 编号
     * @return 返回结果
     */
    @GetMapping("/{id}")
    public ApiResult<Map<String, Object>> info(@PathVariable long id) {
        return ApiResult.success(bankCardService.getInfo(id, StpUtil.getSession().getLong(UID)));
    }

    /**
     * 删除银行卡信息
     * @param id 编号
     * @return 返回结果
     */
    @DeleteMapping("/{id}")
    @OperationLog(desc = "删除银行卡", type = OperationType.DELETE)
    public ApiResult<Map<String, Object>> delete(@PathVariable long id) {
        BankCard delete = new BankCard();
        delete.setId(id);
        delete.setOwnerId(StpUtil.getSession().getLong(UID));
        return ApiResult.result(bankCardService.removeById(delete));
    }

}
